/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.bff.designtime.generator.core;

import com.inspur.edp.bff.designtime.generator.VmGeneratorContext;
import com.inspur.edp.bff.designtime.generator.core.config.BffConfigClassGenerator;
import com.inspur.edp.bff.designtime.generator.core.mgrimpl.extendprovider.CtmExtendProviderGenerator;
import com.inspur.edp.bff.designtime.generator.core.querycondition.DefaultQueryConditionGenerator;
import com.inspur.edp.bff.designtime.generator.core.rtgen.changeset.ChangeConvertorGenerator;
import com.inspur.edp.bff.designtime.generator.core.rtgen.changeset.ViewModelChangeGenerator;
import com.inspur.edp.bff.designtime.generator.core.rtgen.managergenerator.FSMgrGenerator;
import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.cef.entity.config.CefConfig;
import com.inspur.edp.cef.generator.coreGenerator.base.configGenerator.ConfigClassGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.mgrGenerator.ManagerGenerator;
import com.inspur.edp.cef.generator.coreGenerator.entityGenerator.CommonModelCoreGenerator;
import com.inspur.edp.cef.generator.coreGenerator.entityGenerator.CommonModelCoreGeneratorContext;
import com.inspur.edp.bff.designtime.generator.core.propertymapper.EntityPropertyMapperGenerator;

import com.inspur.edp.formserver.viewmodel.GspViewModel;
import java.util.ArrayList;

public class VmCoreGenerator extends CommonModelCoreGenerator {

  private VmGeneratorContext vmGenCtx;

  public VmCoreGenerator(VmGeneratorContext vmGenCtx) {
    super(vmGenCtx, vmGenCtx.getVm());
    this.vmGenCtx = vmGenCtx;
  }

  private VmCoreGenContext getVmCoreGenContext() {
    return (VmCoreGenContext) super.getContext();
  }

  @Override
  protected CommonModelCoreGeneratorContext createEntityCoreGenContext() {
    VmCoreGenContext core = new VmCoreGenContext(vmGenCtx, vmGenCtx.getVm());
    vmGenCtx.setCoreGenContext( core);

    return core;
  }

  protected String getAssemblyName() {
    return VmCoreHelper.getNamespace(vmGenCtx.getVm());
  }

  protected String getDefaultNamespace() {
    return VmCoreHelper.getNamespace(vmGenCtx.getVm());
  }

  @Override
  protected CefConfig buildConfigInfo() {
    CefConfig config = new CefConfig();
    GspViewModel vo = (GspViewModel) getContext().getCefGenContext().getMetadata().getContent();
    config.setID(vo.getGeneratedConfigID());
    config.setBEID(vo.getID());
    config.setDefaultNamespace(vo.getCoreAssemblyInfo().getDefaultNamespace().toLowerCase());
    return config;
  }

  @Override
  protected ArrayList<BaseGenerator> createChildGenerators() {
    ArrayList<BaseGenerator> baseGentors = super.createChildGenerators();
    ArrayList<BaseGenerator> childGens = new java.util.ArrayList<BaseGenerator>();

    //childGens.add(new ActionConfigGenerator(getVmCoreGenContext()));
//    childGens.add(new SourceConfigGenerator(getVmCoreGenContext()));

    String filterCondition=getVmCoreGenContext().getVmGenContext().getVm().getMainObject().getFilterCondition();
    String sortCondition=getVmCoreGenContext().getVmGenContext().getVm().getMainObject().getOrderbyCondition();
    if(!(filterCondition==null||filterCondition.equals("")) || !(sortCondition==null||sortCondition.equals(""))){
      childGens.add(new DefaultQueryConditionGenerator(getVmCoreGenContext()));
    }
    if (baseGentors != null) {
      childGens.addAll(baseGentors);
    }
    if(!vmGenCtx.getVm().getIsVirtual()) {
      childGens.add(new EntityPropertyMapperGenerator(getVmCoreGenContext()));
    }
    if(vmGenCtx.getMetadata().getHeader().isExtendable())
      childGens.add(new CtmExtendProviderGenerator(getVmCoreGenContext()));
    childGens.add(new ChangeConvertorGenerator(getVmCoreGenContext()));
    childGens.add(new ViewModelChangeGenerator(getVmCoreGenContext()));
    return childGens;
  }

//  @Override
//  protected EntityImplGenerator createEntityImplGenerator() {
//    return new VoEntityImplGenerator(getVmCoreGenContext(), vmGenCtx.getVm().getMainObject());
//  }

//  @Override
//  protected EntityMgrImplGenerator createEntityMgrImplGenerator() {
//    return new VmMgrImplGenerator(getVmCoreGenContext());
//  }

  @Override
  protected ManagerGenerator createMgrImplGenerator() {
    return new FSMgrGenerator(getVmCoreGenContext());
  }

  @Override
  protected boolean getHasConfigGenerator() {
    VmCoreGenContext bffCoreGenCtx = getVmCoreGenContext();
    if (bffCoreGenCtx != null && bffCoreGenCtx.getCommonModel() != null) {
      return !(bffCoreGenCtx.getCommonModel().isSimplifyGen() || bffCoreGenCtx.getCommonModel().isSimpBeanConfig());
    }
    return true;
  }

  @Override
  protected String getPackageName() {
    return vmGenCtx.getVm().getCoreAssemblyInfo().getDefaultNamespace();
  }

  @Override
  protected ConfigClassGenerator getConfigGenerator() {
    return new BffConfigClassGenerator(getVmCoreGenContext());
  }

  @Override
  protected void afterGenerate() {
    super.afterGenerate();
  }

  @Override
  protected void beforeGenerate() {
    super.beforeGenerate();
  }
}
